package com.mryang.hds.service.impl;

import com.mryang.hds.configs.AppException;
import com.mryang.hds.entity.EchartsEntity;
import com.mryang.hds.entity.Recovered;
import com.mryang.hds.mapper.ManageECMapper;
import com.mryang.hds.mapper.NeedMapper;
import com.mryang.hds.mapper.PunchRecordMapper;
import com.mryang.hds.mapper.RecoveredMapper;
import com.mryang.hds.service.ManageECService;
import com.mryang.hds.service.ManageService;
import com.mryang.hds.utils.DateUtil;
import com.mryang.hds.utils.ResultCode;
import org.apache.ibatis.annotations.Param;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;

/**
 * @author Genius
 * @version 1.0.0
 * @ClassName ManageECServiceImpl.java
 * @Description TODO
 * @createTime 2023年01月24日 11:39:00
 */

@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
@Service
public class ManageECServiceImpl implements ManageECService {

    @Resource
    ManageECMapper manageECMapper;

    @Resource
    PunchRecordMapper punchRecordMapper;

    @Resource
    NeedMapper needMapper;

    @Resource
    RecoveredMapper recoveredMapper;


    @Override
    @Transactional
    public Map<String, Integer> getUClassify() {
        try {
            HashMap<String,Integer> map = new HashMap<>();
            // 获取管理员人数
            map.put("adu",manageECMapper.getUserstatus0());
            // 获取普通用户
            map.put("odu",manageECMapper.getUserstatus1());
            // 获取医护人员
            map.put("cnu",manageECMapper.getUserstatus2());

            return map;
        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.CHART_FAIL);
        }


    }

    @Transactional
    @Override
    public Map<String, Integer> getUPunch() {
        try {
            HashMap<String,Integer> map = new HashMap<>();
            // 获取普通用户
            map.put("NoPunch",manageECMapper.getUNoPunch());
            // 获取已打卡人数
            map.put("punchOk",manageECMapper.getUPunch());

            return map;
        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.CHART_FAIL);
        }
    }

    @Transactional
    @Override
    public Map<String, Integer> getPunchData() {
        try {
            HashMap<String,Integer> map = new HashMap<>();
            // 未打卡-普通用户
            map.put("UnoPunch",manageECMapper.getPunchByStatusAndPunchInfo(1,0));
            // 健康
            map.put("Uhealthy",manageECMapper.getPunchByStatusAndPunchInfo(1,1));
            // 危险
            map.put("Udanger",manageECMapper.getPunchByStatusAndPunchInfo(1,-1));

            // 未打卡-医护人员
            map.put("DnoPunch",manageECMapper.getPunchByStatusAndPunchInfo(2,0));
            // 健康
            map.put("Dhealthy",manageECMapper.getPunchByStatusAndPunchInfo(2,1));
            // 危险
            map.put("Ddanger",manageECMapper.getPunchByStatusAndPunchInfo(2,-1));


            return map;
        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.CHART_FAIL);
        }
    }


    /**
     * 管理员页面 数据展示页面相关需求
     * @return 数据详情
     */
    @Transactional
    @Override
    public EchartsEntity getDataDisplay() {
        try {
            // HashMap<String,List<Integer>> map = new HashMap<>();
            EchartsEntity echartsEntity = new EchartsEntity();

            // 1、用户量、需求量、消息量、康复量
            ArrayList<Integer> count = new ArrayList<>();
                // 用户量
            count.add(manageECMapper.getUsersCount());
                // 需求量
            count.add(manageECMapper.getNeedsCount());
                // 消息量
            count.add(manageECMapper.getNotifysCount());
                // 康复量
            count.add(manageECMapper.getKangfuCount());

            // map.put("count",count);
            echartsEntity.setCount(count);

            // 2、近14天 健康、异常、未打卡、康复人数
            ArrayList<Integer> jiankang14daysCount = new ArrayList<>();
            ArrayList<Integer> yihcang14daysCount = new ArrayList<>();
            ArrayList<Integer> weidaka14daysCount = new ArrayList<>();
            ArrayList<Integer> kangfu14daysCount = new ArrayList<>();
            ArrayList<String> shijianzhou = new ArrayList<>();

                // 获取14天前的日期
            LocalDate today = LocalDate.now();
            String date14DaysAgo = "";

            for (int i = 13; i >= 0; i--) {
                // 将时间放入集合
                date14DaysAgo = today.minusDays(i).toString();
                shijianzhou.add(date14DaysAgo);

                // 获取前14天每一天的 健康 打卡数量
                jiankang14daysCount.add(punchRecordMapper.getPRCountByDataExceptionPlus("正常", date14DaysAgo));
                // 获取前14天每一天的 异常 打卡数量
                yihcang14daysCount.add(punchRecordMapper.getPRCountByDataExceptionPlus("异常", date14DaysAgo));
                // 获取前14天每一天的 未打卡 打卡数量
                weidaka14daysCount.add(punchRecordMapper.getPRCountByDataExceptionPlus("未打卡", date14DaysAgo));
                // 获取前14天每一天的 康复 数量
                kangfu14daysCount.add(manageECMapper.getKangFuStatusCount(1, date14DaysAgo));
            }

            echartsEntity.setJiankang14daysCount(jiankang14daysCount);
            echartsEntity.setYihcang14daysCount(yihcang14daysCount);
            echartsEntity.setWeidaka14daysCount(weidaka14daysCount);
            echartsEntity.setKangfu14daysCount(kangfu14daysCount);
            echartsEntity.setShijianzhou(shijianzhou);
            // map.put("jiankang14daysCount",jiankang14daysCount);
            // map.put("yihcang14daysCount",yihcang14daysCount);
            // map.put("weidaka14daysCount",weidaka14daysCount);
            // map.put("kangfu14daysCount",kangfu14daysCount);
            // map.put("shijianzhou",shijianzhou);

            return echartsEntity;
        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.CHART_FAIL);
        }
    }

    /**
     * 获取指定医护人员所管理的用户的当日打卡情况
     * @param loginId 医护人员编号
     * @return 相关数量情况
     */
    @Transactional
    @Override
    public Map<String, Integer> getECmain2Data(Integer loginId) {
        try {
            HashMap<String,Integer> map = new HashMap<>();

            // 获取当日 已打卡、未打卡 人数
                // 获取未打卡用户
            map.put("NoPunch",manageECMapper.getUPunchAndDocId(loginId,0));
                // 获取已打卡人数
            map.put("punchOk",manageECMapper.getUPunchAndDocId(loginId,null));
            // 获取当日健康、异常、未打卡详情
                // 获取当天日期
            String time = DateUtil.dateToString(new Date(), "yyyy-MM-dd");
                // 健康
            map.put("Uhealthy",punchRecordMapper.getPunchByStatusAndPunchInfoAndDocId(loginId,"正常",time));
                // 危险
            map.put("Udanger",punchRecordMapper.getPunchByStatusAndPunchInfoAndDocId(loginId,"异常",time));

            return map;

        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.CHART_FAIL);
        }
    }

    /**
     * 获取指定医护人员所管理的用户的需求、康复情况
     * @param loginId 医护人员编号
     * @return 相关数量情况
     */
    @Transactional
    @Override
    public Map<String, Integer> getNeedOrGeLi(Integer loginId) {
        try {
            HashMap<String,Integer> map = new HashMap<>();
            // 获取当天日期
            String time = DateUtil.dateToString(new Date(), "yyyy-MM-dd");
            // 需求相关数据
                // 已结束的需求
            map.put("needOk",needMapper.getNeedCountByIdAndDocId(loginId,2));
                // 待处理的需求
            map.put("need0",needMapper.getNeedCountByIdAndDocId(loginId,0));
                // 审核不通过的需求
            map.put("needNo",needMapper.getNeedCountByIdAndDocId(loginId,-1));
                // 派送中的需求
            map.put("needRun",needMapper.getNeedCountByIdAndDocId(loginId,1));


            // 解除隔离相关
                // 审核通过
            map.put("recoveredOk",recoveredMapper.getRecoveredCountByLoginIdAndStatus(loginId,1));
                // 审核不通过
            map.put("recoveredNo",recoveredMapper.getRecoveredCountByLoginIdAndStatus(loginId,-1));
                // 待审核
            map.put("recovered0",recoveredMapper.getRecoveredCountByLoginIdAndStatus(loginId,0));


            return map;


        }catch (Exception e){
            e.printStackTrace();
            throw new AppException(ResultCode.CHART_FAIL);
        }
    }
}
